package kr.hkit.powerjava.chapter17;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JCheckBox;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class CheckBoxSwingCom extends JPanel implements ItemListener {
	private JPanel panel;
	private static final int SIZE = 3;
	
	JCheckBox[] buttons = new JCheckBox[SIZE];
	String[] fruits = {"Apple","Grape", "Orange"};
	JLabel[] pictureLabel = new JLabel[SIZE];
	ImageIcon[] icon = new ImageIcon[SIZE];

	/**
	 * Create the panel.
	 */
	public CheckBoxSwingCom() {

		initialize();
	}
	private void initialize() {
		setLayout(new GridLayout(0, 4, 0, 0));
		
		panel = new JPanel();
		
		
		for(int i = 0; i<SIZE; i++){
			buttons[i] = new JCheckBox(fruits[i]);
			buttons[i].addItemListener(this);
			pictureLabel[i] = new JLabel(fruits[i] + ".jpg");
			icon[i] = new ImageIcon(fruits[i] + ".jpg");
		}
		
		JPanel checkPanel = new JPanel(new GridLayout(0,1));
		for(int i = 0; i<SIZE ; i++){
			checkPanel.add(buttons[i]);
		}
		
		add(checkPanel);
		for(int i = 0; i<SIZE ; i++){
			add(pictureLabel[i]);
		}
	}
	
	public void itemStateChanged(ItemEvent e){
		ImageIcon image = null;
		
		Object source = e.getItemSelectable();
		for(int i = 0; i< SIZE ; i++){
			if(source == buttons[i]){
				if(e.getStateChange() == ItemEvent.DESELECTED){
					pictureLabel[i].setIcon(null);
				}else{
					pictureLabel[i].setIcon(icon[i]);
				}
			}
		}
	}

}
